package com.itheima.dao;

import com.itheima.domain.Orders;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface IOrdersDao {


    /**
     * 查询所有产品信息
     *
     * @return
     */
    @Select("select * from orders")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "orderNum", column = "orderNum"),
            @Result(property = "orderTime", column = "orderTime"),
            @Result(property = "orderStatus", column = "orderStatus"),
            @Result(property = "peopleCount", column = "peopleCount"),
            @Result(property = "payType", column = "payType"),
            @Result(property = "orderDesc", column = "orderDesc"),
            //查询对应的产品信息：一对多
            @Result(property = "product", column = "productId",
                    one = @One(select = "com.itheima.dao.IProductDao.findProductById", fetchType = FetchType.LAZY))
    })
    List<Orders> findAllOrders();


    /**
     * 查询订单详情
     *
     * @param ordersId 订单ID
     * @return
     */
    @Select("select * from orders where id = #{ordersId}")
    @Results(value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "orderNum", column = "orderNum"),
            @Result(property = "orderTime", column = "orderTime"),
            @Result(property = "orderStatus", column = "orderStatus"),
            @Result(property = "peopleCount", column = "peopleCount"),
            @Result(property = "payType", column = "payType"),
            @Result(property = "orderDesc", column = "orderDesc"),
            //查询对应的产品信息：一对多
            @Result(property = "product", column = "PRODUCTID",
                    one = @One(select = "com.itheima.dao.IProductDao.findProductById", fetchType = FetchType.LAZY)),
            //查询对应的会员信息：一对一
            @Result(property = "member", column = "MEMBERID",
                    one = @One(select = "com.itheima.dao.IMemberDao.findByMemberId",fetchType = FetchType.LAZY)),
            //查询对应的旅客信息：多对多
            @Result(property = "travellers",column ="id",//javaType = java.util.List.class,
                    many = @Many(select = "com.itheima.dao.ITravellerDao.findTravellerByOrders",fetchType = FetchType.LAZY))
    })
    Orders findByOrdersId(String ordersId);


    /**
     * 添加新产品信息
     *
     * @param Orders
     */
    @Insert("INSERT INTO ORDERS (ID, ORDERNUM, ORDERTIME, PEOPLECOUNT, ORDERDESC, PAYTYPE, ORDERSTATUS, PRODUCTID, MEMBERID) VALUES (SYS_GUID(), #{orderNum}, #{orderTime}, #{peopleCount}, #{orderDesc}, #{payType}, #{orderStatus}, #{product.id}, #{member.id})")
    void insertOrders(Orders Orders);

    /**
     * 根据产品id删除产品信息
     *
     * @param id
     */
    @Delete("delete from ORDERS where id = #{id}")
    void deleteOrders(String id);


}
